//
//  MyTableViewCell.m
//  IOS6Demo
//
//  Created by wangfuren on 14-9-15.
//  Copyright (c) 2014年 lichentao. All rights reserved.
//

#import "MyTableViewCell.h"
@interface MyTableViewCell()


@property (nonatomic, strong) UIView *viewCellContent;

@property (nonatomic, strong) UIView *viewVideo;
@property (nonatomic, strong) UIImageView *viewPic;

@property (nonatomic, strong) UILabel *lblVideoTitle;
@property (nonatomic, strong) UILabel *lblVideoDuration;
@end


@implementation MyTableViewCell
@synthesize praiseButton = _praiseButton;
@synthesize shareButton = _shareButton;

/**
 *@brief 将子视图&父视图 must turn off this property first
 */
- (void)setSuperViewAndSubViewNSLayoutConstraint:(UIView *)superView SubView:(UIView *)subView{
    [superView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [subView setTranslatesAutoresizingMaskIntoConstraints:NO];
}

/*
 * @brief 自动布局 subview相对于superView的 top/left/right/bottom距离上下左右
 支持 iOS 6 ＋ 以上系统
 */
- (void)setViewTopLeftRightBottomOffsetFromSubViewToSuperView:(NSArray *)rectArray SubView:(UIView *)subView ToSuperView:(UIView *)superView{
    
    // 必须传值，如果不发生偏移传0
    if (rectArray.count < 4) {
        return;
    }
//    [self setSuperViewAndSubViewNSLayoutConstraint:superView SubView:subView];
    
    [superView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeTop  relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeTop  multiplier:1.0f constant:[[rectArray objectAtIndex:0] floatValue]]];
    [superView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeLeft  relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeLeft  multiplier:1.0f constant:[[rectArray objectAtIndex:1] floatValue]]];
    [superView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeRight  relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeRight  multiplier:1.0f constant:[[rectArray objectAtIndex:2] floatValue]]];
    [superView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeBottom  relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeBottom  multiplier:1.0f constant:[[rectArray objectAtIndex:3] floatValue]]];
}
/*
   @brief 子视图相对于父视图的中心xy比例和父视图的宽高比例(max 1.0f,min0.0f) x | y | width | height
 */
- (void)setViewCenterAndWidthHeightWithSuperViewMultiplier:(NSArray *)rectArray SubView:(UIView *)subView ToSuperView:(UIView *)superView{
    
    [superView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeCenterX  relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeCenterX  multiplier:[[rectArray objectAtIndex:0] floatValue] constant:00]];
    [superView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeCenterY  relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeCenterY  multiplier:[[rectArray objectAtIndex:1] floatValue] constant:00]];
    [superView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeWidth  relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeWidth  multiplier:[[rectArray objectAtIndex:2] floatValue] constant:00]];
    [superView addConstraint:[NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeHeight  relatedBy:NSLayoutRelationEqual toItem:superView attribute:NSLayoutAttributeHeight  multiplier:[[rectArray objectAtIndex:3] floatValue] constant:00]];
}

/**
*  @brief 平级的两个view 相对于父视图 做的约束
*/
- (void)setViewTopLeftRightBottomWithSuperViewWidthHeightMultiplier:(NSArray *)distanceArray SuperView:(UIView *)superView oneView:(UIView *)oneView ToAnortherView:(UIView *)ortherView MultiplierArray:(NSArray *)mulArray{
    
    [superView addConstraint:[NSLayoutConstraint constraintWithItem:oneView attribute:NSLayoutAttributeTop  relatedBy:NSLayoutRelationEqual toItem:ortherView attribute:NSLayoutAttributeTop  multiplier:1.0f constant:[[distanceArray objectAtIndex:0] floatValue]]];
    [superView addConstraint:[NSLayoutConstraint constraintWithItem:oneView attribute:NSLayoutAttributeLeft  relatedBy:NSLayoutRelationEqual toItem:ortherView attribute:NSLayoutAttributeLeft  multiplier:1.0f constant:[[distanceArray objectAtIndex:1] floatValue]]];
    [superView addConstraint:[NSLayoutConstraint constraintWithItem:oneView attribute:NSLayoutAttributeRight  relatedBy:NSLayoutRelationEqual toItem:ortherView attribute:NSLayoutAttributeRight  multiplier:1.0f constant:[[distanceArray objectAtIndex:2] floatValue]]];
    [superView addConstraint:[NSLayoutConstraint constraintWithItem:oneView attribute:NSLayoutAttributeBottom  relatedBy:NSLayoutRelationEqual toItem:ortherView attribute:NSLayoutAttributeBottom  multiplier:1.0f constant:[[distanceArray objectAtIndex:3] floatValue]]];
    
    [superView addConstraint:[NSLayoutConstraint constraintWithItem:oneView attribute:NSLayoutAttributeWidth  relatedBy:NSLayoutRelationEqual toItem:ortherView attribute:NSLayoutAttributeWidth  multiplier:[[mulArray objectAtIndex:0] floatValue] constant:00]];
    
    [superView addConstraint:[NSLayoutConstraint constraintWithItem:oneView attribute:NSLayoutAttributeHeight  relatedBy:NSLayoutRelationEqual toItem:ortherView attribute:NSLayoutAttributeHeight  multiplier:[[mulArray objectAtIndex:1] floatValue] constant:00]];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

//        UIView *bgView = [[UIView alloc] initWithFrame:CGRectZero];
//        bgView.translatesAutoresizingMaskIntoConstraints = NO;
//        bgView.backgroundColor = [UIColor greenColor];
//        [self.contentView addSubview:bgView];
//        
//        [self setViewCenterAndWidthHeightWithSuperViewMultiplier:@[@"1.0",@"1.0",@"1.0",@"1.0"] SubView:bgView ToSuperView:self.contentView];
//        self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
        self.contentView.backgroundColor = [UIColor yellowColor];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.text = @"asdfasdfasfd";
        label.translatesAutoresizingMaskIntoConstraints = NO;
        label.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:label];
        
//
        
        /////////////////////////////////////---------0000
        
        
        
        
        [self setViewTopLeftRightBottomOffsetFromSubViewToSuperView:@[@"10",@"20",@"-100",@"-40"] SubView:label ToSuperView:self.contentView];
        
        UILabel *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.translatesAutoresizingMaskIntoConstraints = NO;
        button.frame = CGRectZero;
        button.backgroundColor = [UIColor blueColor];
        [self.contentView addSubview:button];
        
        NSDictionary *views = NSDictionaryOfVariableBindings(button);
        


        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:label attribute:NSLayoutAttributeBottom multiplier:0.1 constant:00]];
        
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[button(30)]" options:0 metrics:nil views:views]];
        
        [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[button(120)]" options:0 metrics:nil views:views]];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
